<template>
  <div>
    <el-form
      :model="addForm"
      ref="addFormRef"
      label-position="top"
      :rules="rules"
    >
      <el-form-item label="订单编号" prop="orderCode">
        <el-input v-model="addForm.orderCode" placeholder="请输入订单编号" />
      </el-form-item>
      <el-form-item label="收支类型" prop="bType">
        <el-select
          v-model="addForm.bType"
          filterable
          placeholder="请选择收支类型"
        >
          <el-option label="收入" :value="1" />
          <el-option label="支出" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select
          v-model="addForm.orderType"
          filterable
          placeholder="请选择订单类型"
        >
          <el-option label="采购单" :value="1" />
          <el-option label="销售单" :value="2" />
          <el-option label="其他" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="应付/应收" prop="amount">
        <el-input-number
          v-model="addForm.amount"
          placeholder="请输入金额"
          style="width: 100%"
          :precision="2"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="实付/实收" prop="actualAmount">
        <el-input-number
          v-model="addForm.actualAmount"
          placeholder="请输入金额"
          style="width: 100%"
          :precision="2"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="备注" prop="description">
        <el-input
          v-model="addForm.description"
          type="textarea"
          :rows="5"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>

    <div class="fixed bottom-2 right-2">
      <el-button @click="handleCancel(0)">取消</el-button>
      <el-button type="primary" @click="handleSubmit">
        {{ loading ? "提交中..." : "提交" }}
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { cloneDeep } from "lodash";
import { handleApiResponse } from "@/utils/request.js";
import { CreateStatements } from "../../../../../wailsjs/go/api/OrderApi";
import Decimal from "decimal.js";

onMounted(() => {});

const emit = defineEmits(["handleBack"]);

const addFormRef = ref(null);
const addForm = ref({
  orderCode: "",
  amount: null,
  actualAmount: null,
  bType: null,
  orderType: null,
  description: "",
});

const handleCancel = (val = 0) => {
  addFormRef.value.resetFields();
  emit("handleBack", val);
};

const loading = ref(false);
const handleSubmit = () => {
  addFormRef.value.validate((valid) => {
    if (!valid) return;
    let form = cloneDeep(addForm.value);
    console.log(form);

    form.amount = mulTen(form.amount);
    form.actualAmount = mulTen(form.actualAmount);

    handleApiResponse(() => CreateStatements({ statement: form })).then(
      (res) => {
        if (res.code == 0) {
          ElMessage.success("添加成功");
          handleCancel(1);
        }
      }
    );
  });
};

const rules = ref({
  orderCode: [{ required: true, message: "请输入订单编号", trigger: "blur" }],
  amount: [
    { required: true, message: "请输入金额", trigger: "blur" },
    { type: "number", message: "请输入数字" },
  ],
  actualAmount: [
    { required: true, message: "请输入金额", trigger: "blur" },
    { type: "number", message: "请输入数字" },
  ],
  bType: [{ required: true, message: "请选择收支类型" }],
  orderType: [{ required: true, message: "请选择订单类型" }],
});

/** 
  @param {Number} num
  @return 数字乘以100后的结果
*/
function mulTen(num) {
  let initNum = new Decimal(num).mul(new Decimal(100)).toString();
  return +initNum;
}
</script>

<style scoped lang="scss">
</style>